package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.dto.MeetingDto;
import com.yami.shop.bean.model.Meeting;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.MeetingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

@RestController
@RequestMapping("/shop/meeting")
@Tag(name = "会议管理接口")
public class MeetingController {
    @Autowired
    private MeetingService meetingService;

    @GetMapping("/page")
    @Operation(summary = "分页获取会议列表", description = "分页获取会议列表")
    public ServerResponseEntity<IPage<Meeting>> page(@ParameterObject Meeting meeting, PageParam<Meeting> page) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        meeting.setShopId(shopId);
        IPage<Meeting> meetingPage = meetingService.page(page, meeting);
        return ServerResponseEntity.success(meetingPage);
    }


    @GetMapping("/info/{meetingId}")
    @Operation(summary = "根据会议Id获取会议详情", description = "根据会议Id获取会议详情")
    public ServerResponseEntity<MeetingDto> info(@PathVariable("meetingId") Long meetingId) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        MeetingDto meeting = meetingService.getMeetingDtoByShopIdAndMeetingId(shopId, meetingId);
        return ServerResponseEntity.success(meeting);
    }

    @PostMapping
    @Operation(summary = "新增会议", description = "新增会议")
    public ServerResponseEntity<Void> save(@RequestBody @Valid Meeting meeting) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        meeting.setShopId(shopId);
        meeting.setCreateTime(new Date());
        long count = meetingService.count(new LambdaQueryWrapper<Meeting>().eq(Meeting::getMeetingTitle, meeting.getMeetingTitle()).eq(Meeting::getMeetingStatus, 1).eq(Meeting::getShopId, shopId));
        if (count > 0) {
            // 已存在同名会议，不能再进行新增操作
            throw new YamiShopBindException("已存在同名会议！");
        }
        meetingService.addMeeting(meeting);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新会议", description = "更新会议")
    public ServerResponseEntity<Void> update(@RequestBody @Valid Meeting meeting) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        meeting.setShopId(shopId);
        long count = meetingService.count(new LambdaQueryWrapper<Meeting>().eq(Meeting::getMeetingTitle, meeting.getMeetingTitle()).eq(Meeting::getMeetingStatus, 1).eq(Meeting::getShopId, shopId).ne(Meeting::getMeetingId, meeting.getMeetingId()));
        if (count > 0) {
            // 该平台的轮播图已达到最大数量，不能再进行新增操作
            throw new YamiShopBindException("已存在同名会议！");
        }
        meetingService.updateMeeting(meeting);
        meetingService.removeMeetingDtoCacheByMeetingId(meeting.getMeetingId());
        meetingService.removeMeetingListCacheByShopId(shopId);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "根据会议id删除会议", description = "根据会议id删除会议")
    public ServerResponseEntity<Void> delete(@RequestBody Long[] ids) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        meetingService.deleteMeeting(ids);
        meetingService.removeMeetingListCacheByShopId(shopId);
        return ServerResponseEntity.success();
    }

}
